package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.BaseCommons;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.utils.ConstantHolder;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author liguoliang
 * @date 2017-11-16 13:22
 */
@Service
public class TaskService extends BaseCommons {

    /**
     * 每天6点50分执行
     */
    @Scheduled(cron = "0 55 6 * * ?")
    public void notice(){
        logger.info("每天6点55分执行");
        String url = "https://apiv2.readerday.com/api/v2/task/notice/book";
        RestTemplate restTemplate = new RestTemplate();
        ResultBean resultBean = restTemplate.getForObject(url, ResultBean.class);
        if (resultBean.getStatus() == 0){
            Map<String,Object> map = (Map<String,Object>) resultBean.getData();
            int phase = Integer.valueOf(map.get("phase").toString());
            WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(Long.valueOf(map.get("bookid").toString()));
            //首页订阅用户
            taskExecutor.execute(()->templateMsgTaskService.sendSubscribeTemplate(wwdxBookModel,phase));
            //vip上新通知
            //taskExecutor.execute(()->templateMsgTaskService.sendVipPlanTemplate(wwdxBookModel,phase));
            //vip过期通知
            taskExecutor.execute(()->templateMsgTaskService.sendVipExpiresTemplate());
        }
    }
}